using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Data;
using System.Net.Mail;
using Toenda.Rails.Data;
using Toenda.Rails.DataContainer;
using Toenda.TFL.Base;
using Toenda.TFL.Data;

namespace Toenda.Rails {
	namespace System {
		/// <summary>
		/// Class ContentProvider
		/// </summary>
		public class ContentProvider : DBClient {
			private string m_MapPath = "";
			private string m_ConfigMapPath = "";
			private TimeHandler time = new TimeHandler();
			private TextConverter converter = new TextConverter();

			/// <summary>
			/// Default Constructor
			/// </summary>
			public ContentProvider() {
			}

			/// <summary>
			/// Specific constructor
			/// </summary>
			/// <param name="MapPath">Server generated map path</param>
			public ContentProvider(string MapPath) {
				this.m_MapPath = MapPath;
				this.m_ConfigMapPath = this.m_MapPath + "Configuration/Configuration.xml";
			}

			// ---------------------------------------------------------
			// PROPERTIES
			// ---------------------------------------------------------

			/// <summary>
			/// Get or set the server generated map path
			/// </summary>
			public string MapPath {
				get { return this.m_MapPath; }
				set { this.m_MapPath = value; }
			}

			/// <summary>
			/// Get or set the server generated config file map path
			/// </summary>
			public string ConfigMapPath {
				get { return this.m_ConfigMapPath; }
				set { this.m_ConfigMapPath = value; }
			}

			// ---------------------------------------------------------
			// PUBLIC MEMBERS
			// ---------------------------------------------------------

			/// <summary>
			/// Send a vulnerability message
			/// </summary>
			/// <param name="item"></param>
			/// <returns></returns>
			public ErrorType SendVulnerabilityMessage(MessageItem item) {
				// mail to user
				StringBuilder str = new StringBuilder();
				str.Append("<html><head><title>Zappr Mailsystem</title></head><body>");
				str.Append("<span style=\"font-family: verdana, arial, sans-serif;\">");
				str.Append("<h3>Vulnerability Message</h3>");
				str.Append("<div style=\"border-top: 2px solid #393; ");
				str.Append("border-bottom: 1px solid #393; height: 2px;\"></div><br />");
				str.Append("Date: " + item.CreateDate.ToLocalTime().ToString() + "<br />");
				str.Append("<br />");
				str.Append("A user has send us a vulnerability message:");
				str.Append("<br /><br />");
				str.Append("Users email: " + item.EMail + "<br />");
				str.Append("Image ID: " + item.ImageID + "<br />");
				str.Append("Description: " + item.Description + "<br />");
				str.Append("<br />");
				str.Append("<div style=\"border-bottom: 2px solid #393;\"></div><br />");
				str.Append("The zappr.net System");
				str.Append("</span>");
				str.Append("</body></html>");

				// smtp host
				XmlProvider xml = new XmlProvider(this.ConfigMapPath);
				SmtpClient smtp = new SmtpClient(xml.ReadTag("SmtpHost"));

				// mail settings
				MailMessage mail = new MailMessage();
				mail.From = new MailAddress(item.EMail);
				mail.To.Add(new MailAddress(xml.ReadTag("MailAdress")));
				mail.Subject = "Vulnerability Message";
				mail.IsBodyHtml = true;
				mail.Body = str.ToString();

				mail.Headers.Add("content-type", "text/html; charset=us-ascii");

				// send now
				smtp.Send(mail);

				DBContent db = new DBContent();
				return db.SaveVulnerabilityMessage(item);
			}

			/// <summary>
			/// Get a news
			/// </summary>
			/// <param name="ID"></param>
			/// <returns></returns>
			public NewsItem GetNews(string ID) {
				NewsItem news = new NewsItem();
				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_news]");
				str.Append(" WHERE pkid = '" + ID + "'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(dsData.Tables[0].Rows.Count > 0) {
					DataTableReader drData = dsData.CreateDataReader();
					drData.Read();

					news.ID = drData["pkid"].ToString();
					news.Title = drData["title"].ToString();
					news.Text = drData["text"].ToString();
					news.Date = drData.GetDateTime(drData.GetOrdinal("date"));
				}

				return news;
			}

			/// <summary>
			/// Save a new news
			/// </summary>
			/// <param name="news"></param>
			public void SaveNews(NewsItem news) {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_news]");
				str.Append(" WHERE pkid = '" + news.ID +"'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());
				DataRow row = null;

				if(dsData.Tables[0].Rows.Count == 0) {
					row = dsData.Tables[0].NewRow();

					row["pkid"] = Guid.NewGuid().ToString();
				}
				else {
					row = dsData.Tables[0].Rows[0];

					row["pkid"] = news.ID;
				}

				row["title"] = news.Title;
				row["text"] = news.Text;
				row["date"] = news.Date;

				if(dsData.Tables[0].Rows.Count == 0)
					dsData.Tables[0].Rows.Add(row);

				int i = base.DBBaseClass.ExecuteUpdate(dsData, "tbl_news");
			}

			/// <summary>
			/// Get a list of all news
			/// </summary>
			/// <returns></returns>
			public ArrayList GetNewsList() {
				return _GetNewsList(0);
			}

			/// <summary>
			/// Get a list of a specific amount of news
			/// </summary>
			/// <param name="Amount"></param>
			/// <returns></returns>
			public ArrayList GetNewsList(int Amount) {
				return _GetNewsList(Amount);
			}

			/// <summary>
			/// Get a number of news
			/// </summary>
			/// <param name="Amount"></param>
			/// <returns></returns>
			public int GetNewsListAmount() {
				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_news]");
				str.Append(" ORDER BY date DESC");

				return base.DBBaseClass.ExecuteQuery(str.ToString()).Tables[0].Rows.Count;
			}

			/// <summary>
			/// Get a list of all news
			/// </summary>
			/// <param name="Amount"></param>
			/// <returns></returns>
			private ArrayList _GetNewsList(int Amount) {
				ArrayList arr = new ArrayList();
				StringBuilder str = new StringBuilder();

				if(Amount == 0)
					str.Append("SELECT *");
				else
					str.Append("SELECT TOP " + Amount + " *");

				str.Append(" FROM [dbo].[tbl_news]");
				str.Append(" ORDER BY date DESC");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(dsData.Tables[0].Rows.Count > 0) {
					DataTableReader drData = dsData.CreateDataReader();

					while(drData.Read()) {
						NewsItem news = new NewsItem();

						news.ID = drData["pkid"].ToString();
						news.Title = drData["title"].ToString();
						news.Text = drData["text"].ToString();
						news.Date = drData.GetDateTime(drData.GetOrdinal("date"));

						arr.Add(news);
					}
				}

				return arr;
			}

			/// <summary>
			/// Get a content
			/// </summary>
			/// <param name="ID"></param>
			/// <returns></returns>
			public ContentItem GetContent(string ID) {
				ContentItem con = new ContentItem();
				StringBuilder str = new StringBuilder();

				str.Append("SELECT *");
				str.Append(" FROM [dbo].[tbl_content]");
				str.Append(" WHERE id = '" + ID + "'");

				DataSet dsData = base.DBBaseClass.ExecuteQuery(str.ToString());

				if(dsData.Tables[0].Rows.Count > 0) {
					DataTableReader drData = dsData.CreateDataReader();
					drData.Read();

					con.ID = drData["id"].ToString();
					con.Text = drData["content"].ToString();
				}

				return con;
			}
		}
	}
}
